package jym.tan.movielibrary.component.base;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputListener;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import jym.tan.movielibrary.component.Initializer;
import jym.tan.movielibrary.logger.LibraryLogger;
import jym.tan.movielibrary.utils.ClassUtils;

import org.jdesktop.swingx.JXTable;

import com.jtattoo.plaf.ColorHelper;

public abstract class BaseTable<E> extends JXTable implements Initializer, MouseInputListener {

	private static final long serialVersionUID = 7L;
	
	private int rolloverRowIndex = -1;
	
	public BaseTable(Object[][] rowData, Object[] columnNames) {
		super(rowData, columnNames);		
		LibraryLogger.initMessage(ClassUtils.getInstance().getResolvedClassName(this.getClass()));
		initialize();
	}
	
	public BaseTable(TableModel tableModel) {
		super(tableModel);		
		LibraryLogger.initMessage(ClassUtils.getInstance().getResolvedClassName(this.getClass()));
		initialize();
	}
	
	public abstract void initialize();
	
	public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
		Component component = super.prepareRenderer(renderer, row, column);
		Color foreground = getForeground();
		Color background = getBackground();
		if (isRowSelected(row)) {
			foreground = getSelectionForeground();
			background = getSelectionBackground();
		} else if (row == rolloverRowIndex) {
			foreground = getSelectionForeground();
			background = ColorHelper.brighter(getSelectionBackground(), 40);
		} else if (row % 2 == 0) {
			background = ColorHelper.brighter(getParent().getBackground(), 20);
		}
		component.setForeground(foreground);
		component.setBackground(background);
		return component;
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		rolloverRowIndex = -1;
		repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		int row = rowAtPoint(e.getPoint());
		if (row != rolloverRowIndex) {
			rolloverRowIndex = row;
			repaint();
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		
	}
}
